import SparkMD5 from "spark-md5";
//文件分片处理
export const uploadByPieces = ({ file, pieceSize = 1, baseURL, selectedSize,pid, success, uploading, error }) => {
    const randomNumber = Date.now() + String(Math.round(Math.random() * 1000000)); // 生成随机数
    // 上传过程中用到的变量
    const chunkSize = pieceSize * 1024 * 1024;
    const chunkCount = Math.ceil(file.size / chunkSize); // 总片数
    let identifier = "";
    // 计算md5的值
    const countMd5 = async () => {
        return new Promise(function async(resolve, reject) {
            const fileReader = new FileReader();
            if (file.size > selectedSize) {
                // 文件字节大于分割字节
                const md5 = new SparkMD5();
                let index = 0;
                const loadFile = (start, end) => {
                    const slice = file.slice(start, end);
                    fileReader.readAsBinaryString(slice);
                };
                loadFile(0, selectedSize);
                fileReader.onload = (e) => {
                    md5.appendBinary(e.target.result);
                    if (index === 0) {
                        index += selectedSize;
                        loadFile(file.size - selectedSize, file.size);
                    } else {
                        resolve(md5.end());
                    }
                };
            } else {
                fileReader.readAsBinaryString(file);
                fileReader.onload = (e) => {
                    resolve(SparkMD5.hashBinary(e.target.result));
                };
            }
        });
    };
    // 获取file分片
    const getChunkInfo = (file, currentChunk, chunkSize) => {
        let start = (currentChunk - 1) * chunkSize;
        let end = Math.min(file.size, start + chunkSize);
        let chunk = file.slice(start, end);
        return { start, end, chunk };
    };
    // 调用接口上传文件分片
    const uploadChunk = (chunkInfo) => {
        let fetchForm = new FormData();
        fetchForm.append("file", chunkInfo.chunk); // 分块文件传输对象
        fetchForm.append("blob_num", chunkInfo.currentChunk); //第几分片
        fetchForm.append("total_blob_num", chunkInfo.chunkCount); //分片总数
        fetchForm.append("file_name", file.name); // 分块文件名
        // fetchForm.append("pid", pid); // 分块文件名
        // fetchForm.append("taskId", randomNumber); //传输任务ID
        // fetchForm.append("chunkSize", chunkSize); //分块的大小
        // fetchForm.append("identifier", identifier); // 文件唯一标识 md5
        // fetchForm.append("selectedSize", selectedSize); // 用于抽取文件首尾各多少字节作为md5值
        // fetchForm.append("originalFileName", file.name); // 用于抽取文件首尾各多少字节作为md5值
        // fetchForm.append("totalChunks", chunkInfo.chunkCount);   //分片总数
        // return
        baseURL(fetchForm).then((res) => {
            if (res.status == 200) {
                if (res.data.code == 1) {
                    if (chunkInfo.currentChunk < chunkInfo.chunkCount) {
                        const { chunk } = getChunkInfo(file, chunkInfo.currentChunk + 1, chunkSize);
                        uploadChunk({
                            chunk,
                            currentChunk: chunkInfo.currentChunk + 1,
                            chunkCount: chunkInfo.chunkCount,
                        });
                        uploading &&
                        uploading({
                            current: chunkInfo.currentChunk,
                            total: chunkInfo.chunkCount,
                        });
                    }
                    // else {
                    //   // 当总数大于等于分片个数的时候
                    //   if (chunkInfo.currentChunk >= chunkInfo.chunkCount - 1) {

                    //     res.chunk = chunkInfo.currentChunk;
                    //     res.chunkCount = chunkInfo.chunkCount;
                    //     success && success(res);
                    //   }
                    // }
                } else {
                    success && success(res);
                }
            } else {
                error && error(res);
            }
        });
    };
    // 针对每个文件进行chunk处理
    const readChunk = () => {
        // 针对单个文件进行chunk上传
        const { chunk } = getChunkInfo(file, 1, chunkSize);
        uploadChunk({ chunk, currentChunk: 1, chunkCount });
    };
    countMd5().then(function(result) {
        identifier = result;
        readChunk(); // 开始执行代码
    });
};
